/*
 * @Author: xiaosihan
 * @Date: 2023-07-13 17:30:11
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-05-06 02:16:31
 */

import { AmbientLight, DirectionalLight, Object3D, OrthographicCamera, PointLight, SRGBColorSpace, Vector3 } from "three";
import ThreeBase from "three-base";

// 上身效果渲染器
class MockupRenderer extends ThreeBase {

  constructor() {
    super();
    this.setCameraType("orthographicCamera");
  }

  // 环境光
  ambientLight = (() => {
    const ambientLight = new AmbientLight("#fff", 2);
    this.scene.add(ambientLight);
    return ambientLight;
  })();

  //点光源1
  pointLight1 = (() => {
    const pointLight1 = new PointLight("#fff", 2);
    pointLight1.distance = 1000;
    pointLight1.decay = 0;
    pointLight1.position.set(0, 0, 10);
    this.scene.add(pointLight1);
    return pointLight1;
  })();

  // 方向光
  directionalLight = (() => {
    const directionalLight = new DirectionalLight("#fff", 2);
    directionalLight.position.set(-10, 10, 5);
    directionalLight.target.position.set(0, 0, 0);
    directionalLight.castShadow = true;
    directionalLight.shadow.mapSize.set(1024, 1024);
    this.scene.add(directionalLight);
    return directionalLight;
  })();

  setSize(width: number, height: number): void {
    super.setSize(width, height);
    Object.assign(this.orthographicCamera, {
      left: -5,
      bottom: -5,
      top: 5,
      right: 5
    });
  }

  init() {
    this.gridHelper.visible = false;
    this.axesHelper.visible = false;
    this.renderer.outputColorSpace = SRGBColorSpace;
    super.init();
    this.setSize(1024, 1024);
    this.autoRender = false;
    this.resetLookAt({
      center: new Vector3(0, 0, 0),
      position: new Vector3(10, 0, 0)
    });
  }

  // 把目标上身效果模型绘制到cavas 上面
  renderModalToCanvas(modal: Object3D, canvas: HTMLCanvasElement) {
    this.resetLookAt({
      center: new Vector3(0, 0, 0),
      position: new Vector3(0, 0, 10)
    });

    this.scene.add(modal);
    this.renderer.clear();
    this.renderer.render(this.scene, this.orthographicCamera);
    this.renderer.clear();
    this.renderer.render(this.scene, this.orthographicCamera);
    const { width: w, height: h } = canvas;
    const ctx = canvas.getContext("2d");
    if (ctx) {
      const { width: rw, height: rh } = this.renderer.domElement;
      ctx.clearRect(0, 0, rw, rh);
      ctx.drawImage(this.renderer.domElement, 0, 0, rw, rh, 0, 0, w, h);
    }
    this.scene.remove(modal);
  }

  // 渲染
  render(): void {
    // // 在不自动渲染时, needRender=false 就跳过渲染
    // if (!this.autoRender && !this.needRender) {
    //     return;
    // }
    // this.needRender = false;

    // if (this.controls.enabled) {
    //     this.controls.update();
    // }

    // this.renderer.clear();
    // this.renderer.render(this.scene, this.camera);

    // // 渲染场景
    // this.dispatchEvent({ type: "beforeRender" });
    // this.renderer.render(this.scene, this.camera);
  }

}

const mockupRenderer = window.mockupRenderer = new MockupRenderer();

export default mockupRenderer;
